﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Snake
{
    class InputHandler
    {
        public Direction? WaitAndCheckInput(int ms)
        {
            Stopwatch s = Stopwatch.StartNew();
            Direction? dir = null;
            while (s.ElapsedMilliseconds < ms)
            {
                if (Console.KeyAvailable)
                {
                    dir = ReadConsole();
                    Thread.Sleep(20);
                }
            }
            return dir;
        }

        private Direction? ReadConsole()
        {
            ConsoleKeyInfo c = Console.ReadKey();
            switch (c.Key)
            {
                case ConsoleKey.UpArrow:
                    return Direction.Up;
                case ConsoleKey.DownArrow:
                    return Direction.Down;
                case ConsoleKey.LeftArrow:
                    return Direction.Left;
                case ConsoleKey.RightArrow:
                    return Direction.Right;            
                default:
                    return null;
            }            
        }
    }
}
